package com.liu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liu.dto.PatientMapData;
import com.liu.entity.TPatient;
import com.liu.service.TPatientService;
import com.liu.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @ClassName PatientController
 * @Description
 * @Author Lilyan
 * @Date 2022/11/4 17:58
 * @Vsersion V1.0
 * @Projrct Name spring_project
 **/
@Controller
public class PatientController {
    @Autowired
    TPatientService tPatientService;

    @RequestMapping("/getPatientMapData")
    @ResponseBody
    public List<PatientMapData> patientTPatientList() {
        System.out.println(tPatientService.getPatientTPatientMap());
        return tPatientService.getPatientTPatientMap();
    }


    @RequestMapping("/getMiJie")
    @ResponseBody
    public Integer getMiJie() {
        System.out.println(tPatientService.getMiJie());
        return tPatientService.getMiJie();
    }

    @RequestMapping("/getAsymptomatic")
    @ResponseBody
    public Integer getAS() {
        System.out.println(tPatientService.getAsymptomatic());
        return tPatientService.getAsymptomatic();
    }

    @RequestMapping("/getPatientType")
    @ResponseBody
    public List getPatientInfo() {

        System.out.println(tPatientService.getPatientType());
        return tPatientService.getPatientType();
    }

    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        return "hello world";
    }

    /**
     * 查询所有患者
     * @return
     */
    //@RequestMapping("findAll")
    //@ResponseBody
    //public JsonResult<TPatient> findAll(){
    //
    //    List<TPatient> patients = tPatientService.getAll();
    //
    //    System.out.println(patients);
    //
    //    JsonResult jsonResult = JsonResult.ok();
    //    jsonResult.setData(patients);
    //    System.out.println(jsonResult);
    //
    //    return jsonResult;
    //}

    @RequestMapping("findLimitPage")
    @ResponseBody
    public JsonResult<TPatient> findAll(
            @RequestParam(value = "page",required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "limit",required = false,defaultValue = "10") Integer pageSize
    ){

        PageHelper.startPage(pageNum,pageSize);
        List<TPatient> patients = tPatientService.getAll();

        PageInfo<TPatient> pageInfo = new PageInfo<>(patients);

        JsonResult jsonResult = JsonResult.ok();

        jsonResult.setData(pageInfo.getList());
        jsonResult.setCount(pageInfo.getTotal());
        System.out.println(jsonResult);

        return jsonResult;
    }

    @RequestMapping("/add")
    @ResponseBody
    public void add(TPatient tpatient, HttpServletRequest request,
                         HttpServletResponse response) throws IOException {
        System.out.println(tpatient);
        tPatientService.insert(tpatient);
        response.sendRedirect(request.getContextPath()+"/page/home.html");
    }

   /**
   * @Author Lilyan
   * @Description //TODO
   * @Date 11:49 2022/11/7
   * @Param [patient]
   * @return java.lang.String
   **/
    @RequestMapping("/update")
    @ResponseBody
    public String update(TPatient patient){
        System.out.println(patient);

        tPatientService.update(patient);

        return "success";
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public String deleteById(Integer id){
        System.out.println(id);

        tPatientService.deleteById(id);

        return "success";
    }

    @RequestMapping("/deleteByIds")
    @ResponseBody
    public String deleteByIds(Integer[] ids){
        System.out.println(ids);

        tPatientService.deleteByIds(ids);

        return "success";
    }

    @RequestMapping("/getExcel")

    public void getExcel0(Integer[] ids,HttpServletResponse response) throws UnsupportedEncodingException {
        System.out.println(ids);

       tPatientService.writeExcel(ids,response);

    }

    @RequestMapping("/upload")
    @ResponseBody
    public JsonResult upload(MultipartFile file){
        //判断
        if(file != null){
            System.out.println("上传的文件名：" + file.getOriginalFilename());

            tPatientService.save(file);

            JsonResult jsonResult = JsonResult.ok();
            return jsonResult;
        }

        throw new RuntimeException("上传文件不能为空！");
    }







}
